/**
 * Created by jiangtao on 16/6/17.
 */
define(function(require, exports, module) {
    require("fastclick");
    FastClick.attach(document.body);
    //获取dom元素的尺寸
    window.getElementSize = function(element){
       if(element == 'window') {
           return {
               width:window.innerWidth,
               height:window.innerHeight
           };
       };
       if(element) {
           var el = document.querySelector(element);
           if( el ) {
               return {
                   width:el.clientWidth,
                   height:el.clientHeight
               };
           };
       };
       return {
            width: 0 ,
            height : 0
       }
    };

    //类似于$.css的方法
    window.setElementCss = function(el,css){
        var elements = document.querySelectorAll(el);
        if(elements.length <= 0 || ! css) return;
        for(var i = 0 ; i < elements.length ; i ++){
            for (var key in css ){
                elements[i].style[key] = css[key];
            }
        }
    };

    //复制对象
    window.copyObj = function(obj){
        var newObj = {};
        for(var key in obj) {
            newObj[key] = obj [key];
        };
        return newObj;
    };

    //合并的对象
    window.mergeData = function(o,n){
       var oldData = copyObj(o || {}),
           newData = copyObj(n || {});
        for(var  key1 in oldData ) {
            for(var key2 in newData) {
                newData[key1] = oldData[key1];
            };
        }
        return newData;
    };

    //layout 全局
   window.layout = function(el,options){
       options = options || {};
       var defaOptions = {
             tableLiWidth:window.innerWidth / 3,
             activeList:{data:[]},
             nav:[],
             searchModal:{show:false},
             listShow:false,
             footerShow:false,
             handel:{
                 url:staticUrl + '/views/index.html?entery=teacher/addTeacher',
                 text:"成为导师"
             }
       };
       var newOptions = mergeData(defaOptions,options),
           layout   = newOptions.layout,
           template = newOptions.template,
           html     = layout.replace(/<my-view>/,template); //<my-view>为模板中的占位符
       el.innerHTML = html;
       var myVue = new Vue({
         el: el,
         data:newOptions
       });
       myVue.search = function(){
           this.searchModal.show = !this.searchModal.show;
       };
       myVue.$watch('activeList.data',function(){
          setTimeout(function(){
              myVue.listShow = true;
              myVue.footerShow = true;
          },200);
       });
       return myVue;
   };

  //iscroll分页
  window.Pagination = function(el,fun,buttom){
      var fun = fun || function(){},
          btu = buttom || 100,
          startY = 0, direction = null,
          myScroll = new IScroll(el,{
          probeType: 3,
          mouseWheel: true
      });
      myScroll.on('scroll',scrollFun);
      myScroll.on('scrollEnd',scrollFun);
      var toButtom = false;
      function scrollFun(){
          var cy = this.y >> 0,
              ms = cy - startY;
          if(cy <= this.maxScrollY || cy >= 0) return;
          if(Math.abs(ms) < 10) return;
          var dt = ( ms != 0 && ms > 0 ) ? 'down' : 'up';
          if(Math.abs(this.maxScrollY - cy) <= btu) toButtom = true;
          fun(dt,myScroll,toButtom);
          startY = cy;
      };
      setTimeout(function(){
          var toH = getElementSize('*[data-el="top"]').height,
              foH = getElementSize('*[data-el="footer"]').height,
              wiH = getElementSize('window').height,
              scH = wiH - toH - foH;
          setElementCss(el,{ height: scH + 'px'});
         if(myScroll) myScroll.refresh();
      },100);
      return myScroll;
  };

  window.parseDom = function (arg) {
      var objE = document.createElement("div");
      objE.innerHTML = arg;
      return objE.childNodes;
  };

    // 事件绑定
  window.bindEvent = (function() {
        if (window.addEventListener) {
            return function(elem, type, handler) {
                if(elem) elem.addEventListener(type, handler, false);
            }
        } else if (window.attachEvent) {// IE浏览器
            return function(elem, type, handler) {
                if(elem) elem.attachEvent("on" + type, handler);
            }
        }
    })();

    // 事件解除
  window.removeEvent = (function() {
        if (window.removeEventListener) {
            return function(elem, type, handler) {
                if(elem) elem.removeEventListener(type, handler, false);
            }
        } else if (window.detachEvent) {
            return function(elem, type, handler) {
                if(elem) elem.detachEvent("on" + type, handler);
            }
        }
    })();

  //全局loading;

  window.loading = function(){
      function loading(){
          this.loading = null;
          this.id      = 'asujh78hggghjhgbn';
          this.init();
      };
      loading.prototype = {
          init:function(){
              if(this.loading) return;
              this.loading = parseDom('<div class="mask"><div class="loading-conetnt"><div class="load-text"></div></div></div>')[0];
              this.loading.setAttribute('id',this.id);
              document.body.appendChild(this.loading);
              var mask = document.querySelector('.mask');
              window.removeEvent(mask,'touchmove',this._evnetHandel);
              window.bindEvent(mask,'touchmove',this._evnetHandel);
              return this;
          },
          _evnetHandel: function(e){
              e.preventDefault();
          },
          show:function(text){
              this.loading.childNodes[0].childNodes[0].innerText= text || "加载中";
              this.loading.style.display = "block";
              return this;
          },
          close:function(){
              this.loading.style.display = "none";
              return this;
          },
          remove:function(){
              var mask = document.querySelector('.mask');
              window.removeEvent(mask,'touchmove',this._evnetHandel);
              this.loading.style.display = "none";
              this.loading = null;
          }
      };
      return new loading();
  };

 //全局confirm
 window.confirm = function(text,ok,cancel){
     text = text || "";
     ok   = ok || function(){};
     cancel = cancel || function(){};
     var confrimHtml = $('<div class="mask show">' +
         '<div class="confrim_content">' +
         '<div class="confrim_text">'+text+'</div>' +
         '<div class="confrim_btn">' +
         '<div class="confrim_ok">确定</div>' +
         '<div class="confrim_cancel">取消</div>' +
         '</div>' +
         '</div>' +
         '</div>');

     $('body').append(confrimHtml);
     $('.confrim_ok').off("click").on('click',function(){
         confrimHtml.remove();
         ok();
     });
     $('.confrim_cancel').off("click").on('click',function(){
         confrimHtml.remove();
         cancel();
     });
 };

 window.tips = function(text,ok,cancel){

 };

 //全局alert
   window.alert = function(text,ok){
        text = text || "";
        ok   = ok || function(){};
        var confrimHtml = $('<div class="mask show">' +
            '<div class="confrim_content">' +
            '<div class="confrim_text">'+text+'</div>' +
            '<div class="confrim_btn">' +
            '<div class="confrim_ok alert_ok">确定</div>' +
            '</div>' +
            '</div>' +
            '</div>');
        $('body').append(confrimHtml);
        $('.alert_ok').off("click").on('click',function(){
            confrimHtml.remove();
            ok();
        });
    };

});
